package main.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.pojo.User;
import main.servlet.service.RegisterService;
import main.servlet.service.impl.RegisterServiceImpl;
import main.servlet.service.listener.RegisterListener;

/**
 * 处理注册用户
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getAttribute("user");
		System.out.println(user.toString());
		
		///存入数据库
		RegisterService service = new RegisterServiceImpl(new RegisterListener() {
			
			@Override
			public void finishRegister(List<User> userList) {
				System.out.println("成功获取数据库中所有user");
				
				for(User user: userList){
					System.out.println(user.toString());
				}
				
				System.out.println("准备传给SuccessMsgServlet");
				request.setAttribute("userList", userList);
				try {
					request.getRequestDispatcher("/SuccessMsgServlet").forward(request, response);
				} catch (ServletException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		service.registerUserToDB(user);
		
		
	}

}
